<?

class del extends Think {
	
	static function is_empty_dir($path) {
		$i = 0;
		$d = opendir ( $path );
		while ( $f = readdir ( $d ) ) {
			$i ++;
		}
		closedir ( $d );
		return $i > 2 ? 0 : 1;
	}
	
	static function del_dir($path, $del_me = 1) {
		
		if (del::is_empty_dir ( $path )) {
			//是空的
			rmdir ( $path );
		} else {
			//不是空的
			$d = dir ( $path );
			while ( $f = $d->read () ) {
				
				if ($f == "." || $f == "..") {
					continue;
				} elseif (is_dir ( $path . '/' . $f )) {
					//如果是目录
					if (! del::is_empty_dir ( $path . '/' . $f )) {
						//调用自身
						del::del_dir ( $path . '/' . $f );
					}
					if (! $del_me) {
						rmdir ( $path . '/' . $f );
					}
				
				} elseif (is_file ( $path . '/' . $f )) {
					//文件直接删除
					unlink ( $path . '/' . $f );
				}
			}
			$d->close ();
			
			if ($del_me == 1) {
				rmdir ( $path );
			}
		}
		return 1;
	}
	
	static function del_file($url) {
		if (file_exists ( $url )) {
			return unlink ( $url );
		} else {
			return "file not here";
		}
	}
}
?>